import time

from api.bind_assertion_api import BindAssertion
from api.input_api import input_code_one, input_code_two, input_data
from api.login_api import input_user, input_password, select_station, click_button
from api.query_assert_api import QueryAssertion
from api.unbind_assert_api import UnbindAssertion
from config.data import SMOKE_SemiPACK_PACK, SMOKE_PCM_SemiPACK, Domain_Account, KEY, NEGATIVE
from config.settings import APP_CONFIG
from tools.app_manager import AppManager, debug_children
from tools.logger import logger, log_info, log_debug


def semipack_pack_relation_smoke_test():
    """
    冒烟测试：半成品-PACK条码关联成功

    测试用例：A1.1.2_LR_FUNC_008
    :return:
    """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        logger.info("开始半成品-PACK条码绑定成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        login_win = app_manager.connect_to_login()

        logger.info(f"✅ 输入项目号: {SMOKE_SemiPACK_PACK['项目号']}")
        input_user(login_win, SMOKE_SemiPACK_PACK["项目号"])

        logger.info(f"✅ 输入工单号: {SMOKE_SemiPACK_PACK['工单号']}")
        input_password(login_win, SMOKE_SemiPACK_PACK["工单号"])

        logger.info(f"✅ 选择工位站: {SMOKE_SemiPACK_PACK['工位站']}")
        select_station(login_win, SMOKE_SemiPACK_PACK["工位站"])

        logger.info("✅ 点击【确定】按钮")
        click_button(login_win)

        # --- 进入主页面 ---
        main_win = app_manager.connect_to_main()

        # --- 执行绑定操作 ---
        logger.info("📦 开始执行条码绑定流程...")

        logger.info(f"✅ 输入成品条码: {NEGATIVE['成品条码']}")
        input_code_one(main_win, NEGATIVE["成品条码"], "成品条码")
        time.sleep(0.2)  # 可选：等待界面响应

        logger.info(f"✅ 输入半成品条码: {NEGATIVE['半成品条码']}")
        input_code_two(main_win, NEGATIVE["半成品条码"], "半成品条码")

        # --- 断言结果 ---
        logger.info("🔍 正在断言：预期绑定成功")
        BindAssertion.assert_binding_success(main_win)

        # --- 测试通过 ---
        logger.info("✅ 半成品-PACK条码绑定成功-冒烟测试通过！所有步骤执行成功")

    except Exception as e:
        logger.error(f"❌ 半成品-PACK条码绑定成功-冒烟测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 半成品-PACK条码绑定成功-冒烟测试执行结束\n")


def pcm_semipack_relation_smoke_test():
    """
    冒烟测试：PCM-半成品条码关联成功

    测试用例：A1.1.2_LR_FUNC_015
    :return:
    """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        log_info("开始PCM-半成品条码绑定成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        log_info("🔐 登录...")
        login_win = app_manager.connect_to_login()

        log_info(f"✅ 输入项目号: {SMOKE_PCM_SemiPACK['项目号']}")
        input_user(login_win, SMOKE_PCM_SemiPACK["项目号"])

        log_info(f"✅ 输入工单号: {SMOKE_PCM_SemiPACK['工单号']}")
        input_password(login_win, SMOKE_PCM_SemiPACK["工单号"])

        log_info(f"✅ 选择工位站: {SMOKE_PCM_SemiPACK['工位站']}")
        select_station(login_win, SMOKE_PCM_SemiPACK["工位站"])

        log_info("✅ 点击【确定】按钮")
        click_button(login_win)
        log_info("✅ 登录成功")

        # ---进入主界面---
        main_win = app_manager.connect_to_main()

        # --- 执行绑定操作 ---
        log_info("📦 开始执行条码绑定流程...")

        log_info(f"✅ 输入半成品条码: {NEGATIVE['半成品条码']}")
        input_code_one(main_win, NEGATIVE["半成品条码"], "半成品条码")
        time.sleep(0.2)  # 可选：等待界面响应

        logger.info(f"✅ 输入PCM条码: {NEGATIVE['PCM条码']}")
        input_code_two(main_win, NEGATIVE["PCM条码"], "PCM条码")

        # --- 断言结果 ---
        logger.info("🔍 正在断言：预期绑定成功")
        BindAssertion.assert_binding_success(main_win)

        # --- 测试通过 ---
        logger.info("✅ PCM-半成品条码绑定成功-冒烟测试通过！所有步骤执行成功")

    except Exception as e:
        logger.error(f"❌ PCM-半成品条码绑定成功-冒烟测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 PCM-半成品条码绑定成功-冒烟测试执行结束\n")


def semipack_barcode_inquiry_successful():
    """
    冒烟测试：SemiPACK条码查询成功

    涉及测试用例：

    (1)A1.1.2_LR_FUNC_023;

    (2)A1.1.2_LR_FUNC_028;
    :return:
    """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        log_info("开始SemiPACK条码查询成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        log_info("🔐 登录...")
        login_win = app_manager.connect_to_login()

        log_info(f"✅ 输入项目号: {SMOKE_PCM_SemiPACK['项目号']}")
        input_user(login_win, SMOKE_PCM_SemiPACK["项目号"])

        log_info(f"✅ 输入工单号: {SMOKE_PCM_SemiPACK['工单号']}")
        input_password(login_win, SMOKE_PCM_SemiPACK["工单号"])

        log_info(f"✅ 选择工位站: {SMOKE_PCM_SemiPACK['工位站']}")
        select_station(login_win, SMOKE_PCM_SemiPACK["工位站"])

        log_info("✅ 点击【确定】按钮")
        click_button(login_win)
        log_info("✅ 登录成功")

        # ---进入主界面---
        main_win = app_manager.connect_to_main()

        # 在点击查询/解绑后，加个显式等待 + 调试输出
        click_button(main_win, "查询 / 解绑")
        time.sleep(0.3)  # 允许窗口动画完成

        # ---先进行域账号登陆---
        # 找到域账号登陆界面
        domain_account_win = app_manager.connect_to_domain_account()
        log_info("✅ 成功连接到【域账号登陆】窗口")

        # 输入域用户名
        log_info(f"✅ 输入域账号: {Domain_Account['success']['username']}")
        input_user(domain_account_win, Domain_Account['success']['username'], "UserNametxt", "域账号")

        # 输入密码
        log_info(f"✅ 输入域密码: {Domain_Account['success']['password']}")
        input_password(domain_account_win, Domain_Account['success']['password'],"Passwordtxt", "密码")

        # 点击确定
        log_info("🖱 点击【确 定】按钮")
        click_button(domain_account_win, "确 定")
        time.sleep(0.2)
        log_info("✅ 域账号登陆成功")

        # ---登陆成功，进入到关联查询界面---
        relation_inquiry_win = app_manager.connect_to_relation_inquiry()
        log_info("✅ 进入到【关联查询】界面")

        # 打印所有子元素
        debug_children(relation_inquiry_win)

        # 输入半成品条码
        log_info(f"✅ 输入半成品条码: {NEGATIVE['半成品条码']}")
        input_data(relation_inquiry_win, NEGATIVE['半成品条码'])
        time.sleep(0.2)

        # 点击查询按钮
        log_info("🖱 点击【查询】按钮")
        click_button(relation_inquiry_win, "查询")

        # 进行断言，无弹窗即为查询成功
        QueryAssertion.assert_query_success()

    except Exception as e:
        logger.error(f"❌ 冒烟测试：SemiPACK条码查询成功，测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 SemiPACK条码查询成功-冒烟测试执行结束\n")

def pack_barcode_inquiry_successful():
    """
   冒烟测试：成品条码查询成功

   涉及测试用例：

   (1)A1.1.2_LR_FUNC_023;

   (2)A1.1.2_LR_FUNC_038;
   :return:
   """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        log_info("开始成品条码绑定成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        log_info("🔐 登录...")
        login_win = app_manager.connect_to_login()

        log_info(f"✅ 输入项目号: {SMOKE_SemiPACK_PACK['项目号']}")
        input_user(login_win, SMOKE_SemiPACK_PACK["项目号"])

        log_info(f"✅ 输入工单号: {SMOKE_SemiPACK_PACK['工单号']}")
        input_password(login_win, SMOKE_SemiPACK_PACK["工单号"])

        log_info(f"✅ 选择工位站: {SMOKE_SemiPACK_PACK['工位站']}")
        select_station(login_win, SMOKE_SemiPACK_PACK["工位站"])

        log_info("✅ 点击【确定】按钮")
        click_button(login_win)
        log_info("✅ 登录成功")

        # ---进入主界面---
        main_win = app_manager.connect_to_main()

        # 在点击查询/解绑后，加个显式等待 + 调试输出
        click_button(main_win, "查询 / 解绑")
        time.sleep(0.3)  # 允许窗口动画完成

        # ---先进行域账号登陆---
        # 找到域账号登陆界面
        domain_account_win = app_manager.connect_to_domain_account()
        log_info("✅ 成功连接到【域账号登陆】窗口")

        # 输入域用户名
        log_info(f"✅ 输入域账号: {Domain_Account['success']['username']}")
        input_user(domain_account_win, Domain_Account['success']['username'], "UserNametxt", "域账号")

        # 输入密码
        log_info(f"✅ 输入域密码: {Domain_Account['success']['password']}")
        input_password(domain_account_win, Domain_Account['success']['password'], "Passwordtxt", "密码")

        # 点击确定
        log_info("🖱 点击【确 定】按钮")
        click_button(domain_account_win, "确 定")
        time.sleep(0.2)
        log_info("✅ 域账号登陆成功")

        # ---登陆成功，进入到关联查询界面---
        relation_inquiry_win = app_manager.connect_to_relation_inquiry()
        log_info("✅ 进入到【关联查询】界面")

        # 打印所有子元素
        log_debug("🔧 调试模式已开启")
        debug_children(relation_inquiry_win)

        # 输入成品条码
        log_info(f"✅ 输入半成品条码: {NEGATIVE['成品条码']}")
        input_data(relation_inquiry_win, NEGATIVE['成品条码'],"成品条码")
        time.sleep(0.2)

        # 点击查询按钮
        log_info("🖱 点击【查询】按钮")
        click_button(relation_inquiry_win, "查询")

        # 进行断言，无弹窗即为查询成功
        (QueryAssertion.assert_query_success())

    except Exception as e:
        logger.error(f"❌ 冒烟测试：成品条码查询成功，测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 成品条码查询成功-冒烟测试执行结束\n")


def semipack_barcode_unbind_success():
    """
    冒烟测试：SemiPACK条码解绑成功

    涉及测试用例：

    (1)A1.1.2_LR_FUNC_023;

    (2)A1.1.2_LR_FUNC_033;
    :return:
    """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        log_info("开始 SemiPACK条码解绑成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        log_info("🔐 登录...")
        login_win = app_manager.connect_to_login()

        log_info(f"✅ 输入项目号: {SMOKE_PCM_SemiPACK['项目号']}")
        input_user(login_win, SMOKE_PCM_SemiPACK["项目号"])

        log_info(f"✅ 输入工单号: {SMOKE_PCM_SemiPACK['工单号']}")
        input_password(login_win, SMOKE_PCM_SemiPACK["工单号"])

        log_info(f"✅ 选择工位站: {SMOKE_PCM_SemiPACK['工位站']}")
        select_station(login_win, SMOKE_PCM_SemiPACK["工位站"])

        log_info("✅ 点击【确定】按钮")
        click_button(login_win)
        log_info("✅ 登录成功")

        # ---进入主界面---
        main_win = app_manager.connect_to_main()

        # 在点击查询/解绑后，加个显式等待 + 调试输出
        click_button(main_win, "查询 / 解绑")
        time.sleep(0.3)  # 允许窗口动画完成

        # ---先进行域账号登陆---
        # 找到域账号登陆界面
        domain_account_win = app_manager.connect_to_domain_account()
        log_info("✅ 成功连接到【域账号登陆】窗口")

        # 输入域用户名
        log_info(f"✅ 输入域账号: {Domain_Account['success']['username']}")
        input_user(domain_account_win, Domain_Account['success']['username'], "UserNametxt", "域账号")

        # 输入密码
        log_info(f"✅ 输入域密码: {Domain_Account['success']['password']}")
        input_password(domain_account_win, Domain_Account['success']['password'],"Passwordtxt", "密码")

        # 点击确定
        log_info("🖱 点击【确 定】按钮")
        click_button(domain_account_win, "确 定")
        time.sleep(0.2)
        log_info("✅ 域账号登陆成功")

        # ---登陆成功，进入到关联查询界面---
        relation_inquiry_win = app_manager.connect_to_relation_inquiry()
        log_info("✅ 进入到【关联查询】界面")

        # 打印所有子元素
        # debug_children(relation_inquiry_win)

        # 输入半成品条码
        log_info(f"✅ 输入半成品条码: {NEGATIVE['半成品条码']}")
        input_data(relation_inquiry_win, NEGATIVE['半成品条码'])
        time.sleep(0.2)

        # 点击查询按钮
        log_info("🖱 点击【解绑】按钮")
        click_button(relation_inquiry_win, "解绑")

        # 进入"条码关联系统-解绑权限确认"界面
        unbind_permission_win = app_manager.connect_to_key_confirm_form()

        # 等待输入权限密钥窗口出现
        if unbind_permission_win.exists(timeout=5):
            unbind_permission_win.set_focus()
            log_info("✅ 检测到输入权限密钥窗口，输入权限密钥")

            # 输入权限密钥
            log_info(f"✅ 输入权限密钥: {KEY}")
            input_data(unbind_permission_win, KEY, "权限密钥", "Passwordtxt")

            # 点击确定按钮
            log_info("🖱 点击【确定】按钮")
            click_button(unbind_permission_win, "确定")
        else:
            log_info("⚪ 未检测到输入权限密钥窗口")

        # 进行断言
        UnbindAssertion.assert_unbind_success()

    except Exception as e:
        logger.error(f"❌ 冒烟测试：SemiPACK条码解绑失败，测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 SemiPACK条码解绑成功-冒烟测试执行结束\n")


def pack_barcode_unbind_success():
    """
    冒烟测试：成品条码解绑成功

    测试用例：

    (1)A1.1.2_LR_FUNC_023;

    (2)A1.1.2_LR_FUNC_043;
    :return:
    """
    app_manager = AppManager(exe_path=APP_CONFIG["exe_path"])
    try:
        log_info("开始 成品条码解绑成功-冒烟测试...")

        # --- 启动应用 ---
        app_manager.start_app()
        log_info("✅ 启动成功")

        # --- 登录页面操作 ---
        log_info("🔐 登录...")
        login_win = app_manager.connect_to_login()

        log_info(f"✅ 输入项目号: {SMOKE_SemiPACK_PACK['项目号']}")
        input_user(login_win, SMOKE_SemiPACK_PACK["项目号"])

        log_info(f"✅ 输入工单号: {SMOKE_SemiPACK_PACK['工单号']}")
        input_password(login_win, SMOKE_SemiPACK_PACK["工单号"])

        log_info(f"✅ 选择工位站: {SMOKE_SemiPACK_PACK['工位站']}")
        select_station(login_win, SMOKE_SemiPACK_PACK["工位站"])

        log_info("✅ 点击【确定】按钮")
        click_button(login_win)
        log_info("✅ 登录成功")

        # ---进入主界面---
        main_win = app_manager.connect_to_main()

        # 在点击查询/解绑后，加个显式等待 + 调试输出
        click_button(main_win, "查询 / 解绑")
        time.sleep(0.3)  # 允许窗口动画完成

        # ---先进行域账号登陆---
        # 找到域账号登陆界面
        domain_account_win = app_manager.connect_to_domain_account()
        log_info("✅ 成功连接到【域账号登陆】窗口")

        # 输入域用户名
        log_info(f"✅ 输入域账号: {Domain_Account['success']['username']}")
        input_user(domain_account_win, Domain_Account['success']['username'], "UserNametxt", "域账号")

        # 输入密码
        log_info(f"✅ 输入域密码: {Domain_Account['success']['password']}")
        input_password(domain_account_win, Domain_Account['success']['password'],"Passwordtxt", "密码")

        # 点击确定
        log_info("🖱 点击【确 定】按钮")
        click_button(domain_account_win, "确 定")
        time.sleep(0.2)
        log_info("✅ 域账号登陆成功")

        # ---登陆成功，进入到关联查询界面---
        relation_inquiry_win = app_manager.connect_to_relation_inquiry()
        log_info("✅ 进入到【关联查询】界面")

        # 打印所有子元素
        debug_children(relation_inquiry_win)

        # 输入成品条码
        log_info(f"✅ 输入成品条码: {NEGATIVE['成品条码']}")
        input_data(relation_inquiry_win, NEGATIVE['成品条码'])
        time.sleep(0.2)

        # 点击查询按钮
        log_info("🖱 点击【解绑】按钮")
        click_button(relation_inquiry_win, "解绑")

        # 进入"条码关联系统-解绑权限确认"界面
        unbind_permission_win = app_manager.connect_to_key_confirm_form()

        # 等待输入权限密钥窗口出现
        if unbind_permission_win.exists(timeout=5):
            unbind_permission_win.set_focus()
            log_info("✅ 检测到输入权限密钥窗口，输入权限密钥")

            # 输入权限密钥
            log_info(f"✅ 输入权限密钥: {KEY}")
            input_data(unbind_permission_win, KEY, "权限密钥", "Passwordtxt")

            # 点击确定按钮
            log_info("🖱 点击【确定】按钮")
            click_button(unbind_permission_win, "确定")
        else:
            log_info("⚪ 未检测到输入权限密钥窗口")

        # 进行断言
        UnbindAssertion.assert_unbind_success()

    except Exception as e:
        logger.error(f"❌ 冒烟测试：成品条码解绑成功，测试失败: {str(e)}")
        raise
    finally:
        # --- 清理 ---
        logger.info("🛑 正在关闭应用程序...")
        app_manager.close_app()
        logger.info("🔚 成品条码解绑成功-冒烟测试执行结束\n")


if __name__ == '__main__':
    # 1.SemiPACK-成品条码关联成功测试
    # semipack_pack_relation_smoke_test()

    # 2.PCM-半成品条码关联成功测试
    # pcm_semipack_relation_smoke_test()

    # 3.半成品条码查询成功测试
    # semipack_barcode_inquiry_successful()

    # 4.成品条码查询成功测试
    # pack_barcode_inquiry_successful()

    # 5.SemiPACK条码解绑成功测试
    # semipack_barcode_unbind_success()

    # 6.成品条码解绑成功测试
    pack_barcode_unbind_success()

